<?php

class taak_model extends CI_Model {
	public function newTaak(){
		$date = $this->input->post('yadayadadatepicker');
		$tijd = $this->input->post('uur').':'.$this->input->post('minuten').'0:00';
		if($this->uri->segment(6) === FALSE){
			$clientidnr = $this->uri->segment(4);
		}
		else {
			$clientidnr = $this->input->post('clienten');
		}
		
		$new_taak_data = array(			
			'beschrijving' => $this->input->post('beschrijving'),
			'client_id' => $clientidnr,
			'aantal_minuten' => $this->input->post('aantal_minuten'),
			'datum' => $date,
			'onderwerp' => $this->input->post('onderwerp'),
			'tijd' => $tijd,
			'medewerker_id' => $this->input->post('medewerker'),
		);
		
		$this->db->insert('taak', $new_taak_data);
	}
	
	public function getTakenByMedewerker($idMedewerker){
		$this->db->select('*');
		$this->db->join('client', 'client.client_id = taak.client_id');
		$this->db->where('medewerker_id', $idMedewerker);		
		$this->db->order_by("datum", "asc"); 
		$this->db->order_by("tijd", "asc"); 
		$data= $this->db->get('taak');
		return $data;
	}
	
	public function getTaak($taakId){
		$this->db->select('*');
		$this->db->where('taak_id', $taakId);
		$data = $this->db->get('taak');
		return $data;
	}

	//geef een datum (Year month day) en een client nr en je krijgt de taken van die dag van die client terug 
	public function getClientTakenByDate($clientid,$date = NULL){
		if($date == NULL){
			$date = date("Y-m-d");
		}
		$this->db->select('*');
		$this->db->where('taak.client_id', $clientid);
		$this->db->where('taak.datum', $date);
		$this->db->join('client', 'client.client_id = taak.client_id');
		
		$this->db->order_by("tijd", "asc"); 
		$data = $this->db->get('taak');
		return $data;
	}
	
	public function getTakenByClient($id){
		$this->db->select('*');
		$this->db->where('client.client_id', $id);
		$this->db->join('taak', 'client.client_id = taak.client_id');
		$this->db->order_by("datum", "asc"); 
		$this->db->order_by("tijd", "asc"); 
		$data = $this->db->get('client');
		return $data;
	}
	
	public function updateTaak($taakId){
		$date = $this->input->post('yadayadadatepicker');
		$tijd = $this->input->post('uur').':'.$this->input->post('minuten').'0:00';
		$taak_update_data = array(			
			'beschrijving' => $this->input->post('beschrijving'),
			'aantal_minuten' => $this->input->post('aantal_minuten'),
			'datum' => $date,
			'onderwerp' => $this->input->post('onderwerp'),
			'tijd' => $tijd,
			'medewerker_id' => $this->input->post('medewerker'),
		);
		$this->db->where('taak_id', $taakId);
		$this->db->update('taak', $taak_update_data);
	}
	
	public function deleteTaak($taakid){
		$this->db->where('taak_id', $taakid);
		$this->db->delete('taak');
	}
}